{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")\n",
    "# 设置k值\n",
    "k = 4  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
       "       n_clusters=4, n_init=10, n_jobs=None, precompute_distances='auto',\n",
       "       random_state=None, tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "model = KMeans(n_clusters=k)  # 聚类有多少类\n",
    "model.fit(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.80293085 -2.7315146 ]\n",
      " [-2.46154315  2.78737555]\n",
      " [ 2.6265299   3.10868015]\n",
      " [-3.38237045 -2.9473363 ]]\n"
     ]
    }
   ],
   "source": [
    "# 分类中心点坐标\n",
    "centers = model.cluster_centers_\n",
    "print(centers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2\n",
      " 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1\n",
      " 0 3 2 1 0 3]\n"
     ]
    }
   ],
   "source": [
    "# 预测结果\n",
    "result = model.predict(data)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1,\n",
       "       0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3,\n",
       "       2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1,\n",
       "       0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbyklEQVR4nO3dfWxc1ZkG8Ocdxw6euIBIvGYhsY3bNNs0H0WZpY2otNlCVrSloS0V23aIECCs7rYSCFC3wdKilFqLEqmlUqtdWV0QgpFKUnCzjoJaCEX7D10yoYSQhADN2oHSDSZt0gTny553/7iexGPfO74zcz/Ouff5SZbj65k7ZzzwzJlz33OOqCqIiMhembgbQEREjWGQExFZjkFORGQ5BjkRkeUY5ERElpsTx4MuWLBAu7u743hoIiJr7d69+wNVbZ9+PJYg7+7uRrFYjOOhiYisJSIjbsc5tEJEZDkGORGR5RjkRESWY5ATEVmOQU5EZDkGOREBAAp7C+h+pBuZjRl0P9KNwt5C3E0inxjkRITC3gJ6h3oxcnwECsXI8RH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s+OB/LC1G2LQLicizxn7REpA3A0wDuUdW/xN2eMIjIjQDeV9XdIrIm7vbUg0HeAFW93u24iCwHcBWAPSICOMMMr4jINar6fxE2MVBez7dMRG4DcCOA6zR5ExTeBbBoys8LAbwXU1siISLNcEK8oKrPxN2eEF0LYJ2IfAHARQAuFpEnVfXWmNvlGycERUBEhgHkVNXkVdUaIiI3APghgL9T1dG42xM0EZkD5yLudQD+AGAXgG+q6r5YGxYScXogjwP4k6reE3d7ojLZI79fVW+Muy214Bg5BeUnAD4C4DkReVVE/iPuBgVp8kLudwD8Cs6Fvy1JDfFJ1wJYD+Bzk6/nq5M9VjIQe+RERJZjj5yIyHIMciIiyzHIiYgsxyAnIrIcg5yIyHIMciIiyzHIiYgs9//z06Xxx8TjbgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "\n",
    "# 画出各个分类的中心点\n",
    "mark = ['*r', '*b', '*g', '*y']\n",
    "for i,center in enumerate(centers):\n",
    "    plt.plot(center[0],center[1], mark[i], markersize=20)\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD5CAYAAAA6JL6mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAfUklEQVR4nO3de5Bc1X0n8O+vH/PseY+QkIQEQhGBYByRAVNgsIxtIJiCKhfeSpzYaF0prbOOg5MQG4dU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FpVvQ7AWwC+FPJ4PBGRJIC/BvDrAK4B8Jsick24o/JFDsAfqerVAG4C8NmIfF0LPQjgQNiDCJLxQQ7gdwE8qqozAKCqp0Mej9++BuALACK1WKGqz6hqrvThHgBrwxyPD24EcFBVD6nqLIAnUJxgWE1VT6jq/tLvx1EMvDXhjso/IrIWwEcBPB72WIJkQ5BvAnCriOwVkRdE5IawB+QXEbkXwLuq+mrYYwnYpwH8c9iD8GgNgKMLPj6GCAUeAIjI5QA2A9gb7kh89XUUJ0qFsAcSpFTYAwAAEXkOwKoKn3oExTH2ofhj3w0AvisiG9SSdpsaX9ufALijuSPyj9PXpqo/KD3nERR/fN/ZzLEFQCo8ZsX3oBsikgHwfQCfV9WxsMfjBxG5B8BpVX1ZRLaEPZ4gGRHkqvrhap8Tkd8F8GQpuF8SkQKK5yacadb4vKj2tYnIewBcAeBVEQGKpYf9InKjqp5s4hAb5vT/DQBE5AEA9wD4kC0XXgfHAFy24OO1AI6HNBZfiUgaxRDfqapPhj0eH90C4F4RuRtAG4BuEfm2qv52yOPynfF95CLyGQCrVfXPRGQTgH8BsC4CwbCIiBwGMKSqUTjYByJyF4C/BPABVbXioutERFIoLtp+CMC7AH4C4BOq+nqoA/NIirOI/wXgvKp+PuzxBKU0I39IVe8JeyxBsKFG/g0AG0Tk5yguMD0QtRCPqL8C0AXgWRH5qYj8XdgD8qK0cPt7AJ5GcUHwu7aHeMktAD4J4PbS/6eflmawZBHjZ+REROTMhhk5ERE5YJATEVmOQU5EZDkGORGR5RjkRESWY5ATEVmOQU5EZDkGORGR5f4Dho4rUThkdb8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 获取数据值所在的范围\n",
    "x_min, x_max = data[:, 0].min() - 1, data[:, 0].max() + 1\n",
    "y_min, y_max = data[:, 1].min() - 1, data[:, 1].max() + 1\n",
    "\n",
    "# 生成网格矩阵\n",
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),\n",
    "                     np.arange(y_min, y_max, 0.02))\n",
    "\n",
    "z = model.predict(np.c_[xx.ravel(), yy.ravel()])# ravel与flatten类似，多维数据转一维。flatten不会改变原始数据，ravel会改变原始数据\n",
    "z = z.reshape(xx.shape)\n",
    "# 等高线图\n",
    "cs = plt.contourf(xx, yy, z)\n",
    "# 显示结果\n",
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "\n",
    "# 画出各个分类的中心点\n",
    "mark = ['*r', '*b', '*g', '*y']\n",
    "for i,center in enumerate(centers):\n",
    "    plt.plot(center[0],center[1], mark[i], markersize=20)\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
